package net.tv90.shoppingserver.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import net.tv90.shoppingserver.model.dto.CartDTO;
import net.tv90.shoppingserver.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@CrossOrigin
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    CartService cartService;

    @PostMapping("/add")
    @SaCheckLogin
    public String addGoodToCart(CartDTO dto, Model model) {
        cartService.addGoodToCart(dto);
        return "redirect:/";
    }

    @GetMapping("/delete/{id}")
    @SaCheckLogin
    public String deleteCartItem(@PathVariable("id") Integer id, Model model) {
        cartService.removeGoodToCart(id);
        return "redirect:/cart";
    }

    @GetMapping("/update")
    @SaCheckLogin
    public String updateCartItemNum(CartDTO dto, Model model) {
        cartService.updateCartItemNum(dto);
        return "redirect:/cart";
    }
}
